﻿$(function () {
    $("#year").ligerComboBox({
        width: 60,
        value: (new Date).getFullYear(),
        valueFieldID: 'hdnYear'
    });
    $("#quarter").ligerComboBox({
        width: 80,
        cancelable: false,
        valueFieldID: 'hdnQuarter',
        data: [
              { id: 0, text: "全部" },
              { id: 1, text: "第一季度" },
              { id: 2, text: "第二季度" },
              { id: 3, text: "第三季度" },
              { id: 4, text: "第四季度" }
        ],
        onSelected: function (value) {
            var monthJson = [{ id: 0, text: "全部" }];
            switch (value) {
                case "1":
                    monthJson.push({ id: 1, text: "1" }, { id: 2, text: "2" }, { id: 3, text: "3" });
                    break;
                case "2":
                    monthJson.push({ id: 4, text: "4" }, { id: 5, text: "5" }, { id: 6, text: "6" });
                    break;
                case "3":
                    monthJson.push({ id: 7, text: "7" }, { id: 8, text: "8" }, { id: 9, text: "9" });
                    break;
                case "4":
                    monthJson.push({ id: 10, text: "10" }, { id: 11, text: "11" }, { id: 12, text: "12" });
                    break;
                default:
                    monthJson.push(
                                   { id: 1, text: "1" }, { id: 2, text: "2" }, { id: 3, text: "3" },
                                   { id: 4, text: "4" }, { id: 5, text: "5" }, { id: 6, text: "6" },
                                   { id: 7, text: "7" }, { id: 8, text: "8" }, { id: 9, text: "9" },
                                   { id: 10, text: "10" }, { id: 11, text: "11" }, { id: 12, text: "12" }
                                  );
            }
            $("#month").ligerComboBox({ width: 60, cancelable: false, valueFieldID: 'hdnMonth' }).setData(monthJson);
            $("#month").ligerComboBox().setValue("0");
        }
    });

    var month = new Date().getMonth();
    if (month < 3) {
        $("#quarter").ligerComboBox().setValue("1");
    } else if (month < 6) {
        $("#quarter").ligerComboBox().setValue("2");
    } else if (month < 9) {
        $("#quarter").ligerComboBox().setValue("3");
    } else if (month < 12) {
        $("#quarter").ligerComboBox().setValue("4");
    }

    $('#btnSearch').click(function () {
        search();
    });

    $('#btnAdd').click(function () {
        window.location.href = "/AdverseEvent/EditPressureSoreEvent/Index?ram" + Math.random();
    });

    $('#btnSearch').click().focus();
});

//加载列表
function initTable(queryData, pageNumber) {
    $("#maingrid").ligerGrid({
        height: '100%',
        url: '/AdverseEvent/PressureSoreEventList/Search?ram=' + Math.random(),
        parms: queryData,
        page: pageNumber,
        pageSize: 30,
        rownumbers: true,
        heightDiff: -5,
        columns: [
        { display: '报告时间', name: 'CreateTime' },
        {
            display: '事件类型', name: 'EventTypeName', render: function (row) {
                return row.EventTypeName + (row.YsType == "" ? "（院外）" : row.YsType);
            }
        },
        { display: '报告人', name: 'NurseName' },
        { display: '发生单位', name: 'AreaName' },
        { display: '不良事件发生时间', name: 'OccurrTime' },
        {
            display: '报告状态', name: '', minWidth: 100, render: function (row) {
                var html = '';
                if (row.Status == 0) {
                    html = '草稿';
                }
                else if (row.Status == 1) {
                    html = '在报';
                }
                else if (row.Status == 2) {
                    html = '已上报';
                }
                else if (row.Status == 3) {
                    html = '上报退回';
                }
                else if (row.Status == 4) {
                    html = '审核通过';
                }
                else if (row.Status == 5) {
                    html = '审核不通过';
                }
                return html;
            }
        },
        {
            display: '操作', name: '', render: function (row) {
                var html = '';
                if (row.Status == 0) {
                    html = '<div style="heigth:100%;width:100px;text-align: left;margin:0px auto;"><a href="/AdverseEvent/EditPressureSoreEvent/Index?id=' + row.ID + '&pageType=0&status=' + row.Status + '" class="a-edit">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" class="a-del" onclick="del(' + row.ID + ')">删除</a></div>';
                }
                else if (row.Status == 3) {
                    html = '<div style="heigth:100%;width:100px;text-align: left;margin:0px auto;"><a href="/AdverseEvent/EditPressureSoreEvent/Index?id=' + row.ID + '&pageType=0&status=' + row.Status + '" class="a-edit">编辑</a>';
                }
                else {
                    html = '<div style="heigth:100%;width:100px;text-align: left;margin:0px auto;"><a href="/AdverseEvent/EditPressureSoreEvent/Index?id=' + row.ID + '&pageType=0&status=' + row.Status + '" class="a-edit">查看</a></div>';
                }
                return html;
            }
        }]
    });
    GridNoData($("#maingrid"));
}

//搜索
function search(pageNumber) {
    var queryData = {
        year: $("#hdnYear").val(),
        quarter: $("#hdnQuarter").val(),
        month: $("#hdnMonth").val()
    }
    initTable(queryData, pageNumber);
}

//删除
function del(id) {
    $.ligerDialog.confirm('确定删除？', function (value) {
        if (value) {
            var url = "/AdverseEvent/AdverseEventList/Delete";
            $.ajax({
                url: url,
                type: "POST",
                cache: false,
                data: { id: id },
                success: function (data) {
                    var d = $.parseJSON(data);
                    if (d.Result == 1) {
                        $.ligerDialog.success("删除成功");
                        $('#btnSearch').click();
                    }
                    else if (d.Result == 0) {
                        $.ligerDialog.error(d.Body);
                    }
                    else {
                        $.ligerDialog.error("程序错误");
                    }
                },
                error: function () {
                    $.ligerDialog.error("程序错误");
                }
            });
        }
    });
}
